/*=========================================================================
project:       External Engagement
Author:        Aila Matanock
Dependencies:  UC Berkeley
---------------------------------------------------------------------------
Creation Date: 2012      
Modification Date: 11/16/2017
Do-file version: 4   
References: Initial data universe drawn from UCDP Peace Agreement Dataset (modified) and UCDP Conflict Termination Dataset (also modified)
Master data are in F1975Bookmergedplus@5.dta, Base_Data_Rerun June 17 Update@1.dta, and postconflict_elections_legislative_merged.dta (posted versions including needed variables are noted in code as needed)          
Output: Models and graphs
===========================================================================*/

***Need packages spost13_do, tabout, outreg2, mat2txt

*Import dataset

use Matanock_External Engagement_Agreements.dta


*================Figure 1: Peace Agreements over Time=================*

gen dum=1
twoway histogram payear if dum, start(1975) width(1) freq bfcolor(none) blcolor(black) || histogram payear if propartNC, freq start(1975) width(1) barw(.8) bfcolor(gray) blcolor(black) legend(order(1 "All Agreements" 2 "Electoral Participation Provisions") pos(2) ring(0) col(1))


*===========Table 2: Inclusion of Electoral Participation Provisions in Settlements=============*

*Model 1
logit propartNC coldwar nexlem, cluster(ccode)

*Model 2
logit propartNC regdg, cluster(ccode)

*Model 3
logit propartNC coldwar oild milaidd colbrit colfra, cluster(ccode)

*Model 4
logit propartNC coldwar nexlem oild milaidd colbrit colfra, cluster(ccode)

*Model 5
logit propartNC coldwar regdg oild milaidd colbrit colfra, cluster(ccode)

*Average Marginal Effects
quietly logit propartNC coldwar nexlem oild milaidd colbrit colfra, cluster(ccode) nolog
margins, dydx(nexlem oild milaidd colbrit colfra)

*===========Figure 2: Predicted Probability of Electoral Participation Provisions in Settlements=============*

preserve

	logit propartNC coldwar nexlem oild milaidd colbrit colfra, cluster(ccode)

	margins, at(nexlem==(0(.1)1) (median) coldwar oild milaidd colbrit colfra) predict(outcome(==1)) atmeans vsquish post

	mat drop _all
	mat t=J(11,3,.)
	
	/* get the 11 "at" values    */
	mat a = (0\0.1\0.2\0.3\0.4\0.5\0.6\0.7\0.8\0.9\1)                        
		forvalues i=1/11 {
		  * get probability estimates 
		  mat t[`i',1] = _b[`i'._at]  
		  * compute lower limit       
		  mat t[`i',2] = _b[`i'._at] - 1.96*_se[`i'._at] 
		  * compute upper limit
		  mat t[`i',3] = _b[`i'._at] + 1.96*_se[`i'._at]   
		}
	
	* Predicted probabilities 
	*Computes and stores the predicted probs (AT THOSE SPECIFIC VALUES OF NEXLEM -- 0.1 0.2 0.3 ...)
	
	cap mat drop t tmp sv
	mat t=t,a            
	mat colnames t = prob ll ul at                     
	svmat t, names(col)                                
	matrix tmp = r(table) 
	matrix sv = nullmat(sv) , tmp[1..4,1...] 
		
	twoway (rarea ll ul at, bcolor(gs13))(line prob at, lcolor(ebblue) lwidth(medthick)), legend(off) xtitle(nexlem) ytitle(Predicted Probability) title(Predicted probability for electoral provisions)

restore

************Switch dataset************

use Matanock_External Engagement_Conflicts.dta

replace outcome_a_first=0 if outcome_a_first==.
gen keepobs=1 if payear >=1975 & payear<=2005
drop if altcensor1==1

*Creating polynomials to model time dependence in multinomial logit. 

sort iden payear

*bys iden: gen duration = _n
gen logdur = log(duration)
gen duration2 = duration^2
gen duration3 = duration^3 

* Dropping duplicates: 

duplicates list ccode payear nsideb outcome_a_first

list ccode payear nsideb outcome_a_first duration if ccode == 700 &  payear== 1993 & nsideb=="Hizb-i Islami-yi Afghanistan - Hekmatyar faction"
list ccode payear nsideb outcome_a_first duration if ccode == 437 &  payear== 2003 & nsideb=="MPIGO"
list ccode payear nsideb outcome_a_first duration if ccode == 437 &  payear== 2003 & nsideb=="MJP"

*Drop the ones with the lowest duration: 
drop in 3
drop in 7
drop in 8


*============Table 3: Conflict Termination through Electoral Participation Provisions=============*

*Model 6 
mlogit outcome_a_first coldwar nexlem duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)

*Model 7 
mlogit outcome_a_first coldwar regdg duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)

*Model 8 - SE is off by 0.01 (I'm getting 0.3147, table in paper has 0.32) 
mlogit outcome_a_first coldwar oild milaidd colbrit colfra duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)

*Model 9
mlogit outcome_a_first coldwar nexlem oild milaidd colbrit colfra duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)

*Model 10
mlogit outcome_a_first coldwar regdg oild milaidd colbrit colfra duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)

*Average Marginal Effects
mlogit outcome_a_first coldwar nexlem oild milaidd colbrit colfra duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)rrr


*=========Figure 3. Predicted Probability of Conflict Termination===========*
*===================through Electoral Participation Provisions==============*

preserve

	mlogit outcome_a_first coldwar nexlem oild milaidd colbrit colfra duration duration2 duration3 if keepobs==1, robust cluster(ccode) base(0)

	margins, at(nexlem==(0(.1)1) (median) coldwar oild milaidd colbrit colfra) predict(outcome(8)) atmeans vsquish post

	mat drop _all
	mat t=J(11,3,.)
	
	/* get the 11 "at" values    */
	mat a = (0\0.1\0.2\0.3\0.4\0.5\0.6\0.7\0.8\0.9\1)                        
		forvalues i=1/11 {
		  * get probability estimates 
		  mat t[`i',1] = _b[`i'._at]  
		  * compute lower limit       
		  mat t[`i',2] = _b[`i'._at] - 1.96*_se[`i'._at] 
		  * compute upper limit
		  mat t[`i',3] = _b[`i'._at] + 1.96*_se[`i'._at]   
		}
	
	* Predicted probabilities 
	*Computes and stores the predicted probs (AT THOSE SPECIFIC VALUES OF NEXLEM -- 0.1 0.2 0.3 ...)
	
	cap mat drop t tmp sv
	mat t=t,a            
	mat colnames t = prob ll ul at                     
	svmat t, names(col)                                
	matrix tmp = r(table) 
	matrix sv = nullmat(sv) , tmp[1..4,1...] 
		
	twoway (rarea ll ul at, bcolor(gs13))(line prob at, lcolor(ebblue) lwidth(medthick)), legend(off) xtitle(nexlem) ytitle(Predicted Probability) title(Predicted probability for electoral provisions)

restore


*=======Table 4. Other Correlates of Electoral Participation Provisions across Model Controls=======*

*Created in Excel based on Figures at the end of the Supporting Information
			
		
			
			
